<?php
namespace app\common\taglib;
use think\template\TagLib;

/**
 * 系统内置标签库
 * Class NavTag
 * @package app\common\taglib
 */
class System extends  TagLib
{
    protected $tags   =  [
        'nav'        => ['attr' => 'id,item,field,status,pid,limit,order,nav_id','close' => 1],
        'notice'     => ['attr' => 'id,item,field,status,pid,limit,order','close' => 1],
        'slide'      => ['attr' => 'id,item,field,status,limit,order,dir','close' => 1],
        'article'    => ['attr' => 'id,item,field,status,limit,order,comment,top,reco','close' => 1],
        'ad'         => ['attr' => 'id,item,field,status,limit,order,type','close' => 1],
        'link'       => ['attr' => 'id,item,field,status,limit,order,type','close' => 1],
        'about'      => ['attr' => 'id,item,field,status,limit,order,app_type','close' => 1],
        'navloca'    => ['attr' => 'id,is_main,field,limit,order','close' => 1],

    ];


    /**
     * @Notes:导航
     * @User: chenping
     * @email: 375129701@qq.com
     * @param $tag = [
     *      id : 导航id,
     *      status : 状态，
     *      pid : 子集
     *      field : 字段
     *      limit : 条数，
     *      order : "排序"
     * ]
     * @param $content
     * @return string
     */
    public function tagNavloca($tag, $content)
    {
        $name = $tag['item'];
        $sql = [];
        $field = "*";
        $limit = 6;
        $order = "id DESC";
        //根据id 读取广告
        if(isset($tag['id']) && !empty($tag['id']))
        {
            array_push($sql,"id in ({$tag['id']})");
        }else{
            array_push($sql,"is_main=1");
        }
        //读取所需得字段
        if(isset($tag['field']) && !empty($tag['field'])) $field=$tag['field'];
        //条数
        if(isset($tag['limit']) && !empty($tag['limit'])) $limit = $tag['limit'];
        //排序
        if(isset($tag['order']) && !empty($tag['order'])) $order = $tag['order'];

        if($sql)
        {
            $where = implode(" and ",$sql);
        }else{
            $where="";
        }
        $parse = '<?php
             $model = new \app\admin\model\system\NavMenuModel();
             $__LIST__=  $model->where("'.$where.'")->field("'.$field.'")->limit("'.$limit.'")
                ->order("'.$order.'")
                ->select();
         ?>';
        $parse .= '{volist name="__LIST__" id="'. $name .'"}';
        $parse .= $content;
        $parse .= '{/volist}';
        return $parse;
    }

    /**
     * @Notes:导航
     * @User: chenping
     * @email: 375129701@qq.com
     * @param $tag = [
     *      id : 导航id,
     *      status : 状态，
     *      pid : 子集
     *      field : 字段
     *      limit : 条数，
     *      order : "排序"
     * ]
     * @param $content
     * @return string
     */
    public function tagNav($tag, $content)
    {
        $name = $tag['item'];
        $sql = [];
        $field = "*";
        $limit = 6;
        $order = "id DESC";
        //根据id 读取广告
        if(isset($tag['id']) && !empty($tag['id'])) array_push($sql,"id in ({$tag['id']})") ;
        //读取所需得字段
        if(isset($tag['field']) && !empty($tag['field'])) $field=$tag['field'];
        //读取状态
        if(isset($tag['status']) && !empty($tag['status'])) array_push($sql,"status={$tag['status']}");
        //读取子集数据
        if(isset($tag['pid']) && !empty($tag['pid'])) array_push($sql,"pid in {$tag['pid']}");
        //根据位置读取
        if(isset($tag['nav_id']) && !empty($tag['nav_id'])) array_push($sql,"nav_id = {$tag['nav_id']}");
        //条数
        if(isset($tag['limit']) && !empty($tag['limit'])) $limit = $tag['limit'];
        //排序
        if(isset($tag['order']) && !empty($tag['order'])) $order = $tag['order'];

        if($sql)
        {
            $where = implode(" and ",$sql);
        }else{
            $where="";
        }
        $parse = '<?php
             $model = new \app\admin\model\system\Nav();
             $__LIST__=  $model->where("'.$where.'")->field("'.$field.'")->limit("'.$limit.'")
                ->order("'.$order.'")
                ->select();
         ?>';
        $parse .= '{volist name="__LIST__" id="'. $name .'"}';
        $parse .= $content;
        $parse .= '{/volist}';
        return $parse;
    }

    /**
     * @Notes:公告/ 通告
     * @User: chenping
     * @email: 375129701@qq.com
     * @param $tag = [
     *      id : 导航id,
     *      status : 状态，
     *      pid : 子集,
     *      field : 字段,
     *     limit : 条数，
     *      order : "排序"
     * ]
     * @param $content
     * @return string
     */
    public function tagNotice($tag, $content)
    {
        $name = $tag['item'];
        $sql = [];
        $field = "*";
        $limit = 6;
        $order = "id DESC";
        //根据id 读取广告
        if(isset($tag['id']) && !empty($tag['id'])) array_push($sql,"id in ({$tag['id']})") ;
        //读取所需得字段
        if(isset($tag['field']) && !empty($tag['field'])) $field=$tag['field'];
        //读取状态
        if(isset($tag['status']) && !empty($tag['status'])) array_push($sql,"post_status={$tag['status']}");
        //读取子集数据
        if(isset($tag['pid']) && !empty($tag['pid'])) array_push($sql,"parent_id in {$tag['pid']}");
        //条数
        if(isset($tag['limit']) && !empty($tag['limit'])) $limit = $tag['limit'];
        //排序
        if(isset($tag['order']) && !empty($tag['order'])) $order = $tag['order'];
        if($sql)
        {
            $where = implode(" and ",$sql);
        }else{
            $where="";
        }
        $parse = '<?php
             $model = new \app\admin\model\system\Notice();
             $__LIST__=  $model->where("'.$where.'")->field("'.$field.'")->limit("'.$limit.'")
                ->order("'.$order.'")
                ->select();
         ?>';
        $parse .= '{volist name="__LIST__" id="'. $name .'"}';
        $parse .= $content;
        $parse .= '{/volist}';
        return $parse;
    }


    /**
     * @Notes:幻灯片
     * @User: chenping
     * @email: 375129701@qq.com
     * @param $tag = [
     *      id : 导航id,
     *      status : 状态，
     *      field : 字段,
     *     limit : 条数，
     *      order : "排序"
     * ]
     * @param $content
     * @return string
     */
    public function tagSlide($tag, $content)
    {
        $name = $tag['item'];
        $sql = [];
        $field = "*";
        $limit = 6;
        $order = "id DESC";
        //根据id 读取广告
        if(isset($tag['id']) && !empty($tag['id'])) array_push($sql,"id in ({$tag['id']})") ;
        //读取所需得字段
        if(isset($tag['field']) && !empty($tag['field'])) $field=$tag['field'];
        //读取状态
        if(isset($tag['status']) && !empty($tag['status'])) array_push($sql,"status={$tag['status']}");
        //目录id
        if(isset($tag['dir']) && !empty($tag['dir'])) array_push($sql,"slide_id={$tag['dir']}");
        //条数
        if(isset($tag['limit']) && !empty($tag['limit'])) $limit = $tag['limit'];
        //排序
        if(isset($tag['order']) && !empty($tag['order'])) $order = $tag['order'];
        if($sql)
        {
            $where = implode(" and ",$sql);
        }else{
            $where="";
        }
        $parse = '<?php
             $model = new \app\admin\model\system\SlideItem();
             $__LIST__=  $model->where("'.$where.'")->field("'.$field.'")->limit("'.$limit.'")
                ->order("'.$order.'")
                ->select();
         ?>';
        $parse .= '{volist name="__LIST__" id="'. $name .'"}';
        $parse .= $content;
        $parse .= '{/volist}';
        return $parse;
    }


    /**
     * @Notes:幻灯片
     * @User: chenping
     * @email: 375129701@qq.com
     * @param $tag = [
     *      id : 导航id,
     *      status : 状态，
     *      field : 字段,
     *     limit : 条数，
     *      order : "排序"
     *      comment : "是否允许评论"
     *      top : "是否置顶"
     *      reco : "是否推荐"
     * ]
     * @param $content
     * @return string
     */
    public function tagArticle($tag, $content)
    {
        $name = $tag['item'];
        $sql = [];
        $field = "*";
        $limit = 6;
        $order = "id DESC";
        $with  = "";
        //根据id 读取广告
        if(isset($tag['id']) && !empty($tag['id'])) array_push($sql,"id in ({$tag['id']})") ;
        //读取所需得字段
        if(isset($tag['field']) && !empty($tag['field'])) $field=$tag['field'];
        //读取状态
        if(isset($tag['status']) && !empty($tag['status'])) array_push($sql,"status={$tag['status']}");
        //条数
        if(isset($tag['limit']) && !empty($tag['limit'])) $limit = $tag['limit'];
        //排序
        if(isset($tag['order']) && !empty($tag['order'])) $order = $tag['order'];
        //是否允许评论
        if(isset($tag['comment']) && !empty($tag['comment']))  array_push($sql,"comment_status={$tag['comment']}");
        //是否置顶
        if(isset($tag['top']) && !empty($tag['top']))  array_push($sql,"is_top={$tag['top']}");
        //是否推荐
        if(isset($tag['reco']) && !empty($tag['reco']))  array_push($sql,"recommended={$tag['reco']}");
        //属性
        if(isset($tag['with'])) $with = $tag['with'];
        if($sql)
        {
            $where = implode(" and ",$sql);
        }else{
            $where="";
        }
        $parse = '<?php
             $model = new \app\post\model\Post();
             $__LIST__=  $model->where("'.$where.'")->field("'.$field.'")->limit("'.$limit.'")
                ->with("'.$with.'")
                ->order("'.$order.'")
                ->select();
         ?>';
        $parse .= '{volist name="__LIST__" id="'. $name .'"}';
        $parse .= $content;
        $parse .= '{/volist}';
        return $parse;
    }


    /**
     * @Notes:导航
     * @User: chenping
     * @email: 375129701@qq.com
     * @param $tag = [
     *      id : 导航id,
     *      status : 状态，
     *      field : 字段
     *      limit : 条数，
     *      order : "排序",
     *      type : "形式"
     * ]
     * @param $content
     * @return string
     */
    public function tagAd($tag, $content)
    {
        $name = $tag['item'];
        $sql = [];
        $field = "*";
        $limit = 6;
        $order = "id DESC";
        //根据id 读取广告
        if(isset($tag['id']) && !empty($tag['id'])) array_push($sql,"id in ({$tag['id']})") ;
        //读取所需得字段
        if(isset($tag['field']) && !empty($tag['field'])) $field=$tag['field'];
        //读取状态
        if(isset($tag['status']) && !empty($tag['status'])) array_push($sql,"status={$tag['status']}");
        //读取子集数据
        if(isset($tag['pid']) && !empty($tag['pid'])) array_push($sql,"pid in {$tag['pid']}");
        //条数
        if(isset($tag['limit']) && !empty($tag['limit'])) $limit = $tag['limit'];
        //排序
        if(isset($tag['order']) && !empty($tag['order'])) $order = $tag['order'];
        //形式
        if(isset($tag['type']) && !empty($tag['type'])) array_push($sql,"type={$tag['type']}");
        if($sql)
        {
            $where = implode(" and ",$sql);
        }else{
            $where="";
        }
        $parse = '<?php
             $model = new \app\admin\model\system\Ad();
             $__LIST__=  $model->where("'.$where.'")->field("'.$field.'")->limit("'.$limit.'")
                ->order("'.$order.'")
                ->select();
         ?>';
        $parse .= '{volist name="__LIST__" id="'. $name .'"}';
        $parse .= $content;
        $parse .= '{/volist}';
        return $parse;
    }

    /**
     * @Notes:导航
     * @User: chenping
     * @email: 375129701@qq.com
     * @param $tag = [
     *      id : 导航id,
     *      status : 状态，
     *      field : 字段
     *      limit : 条数，
     *      order : "排序",
     *      type : "形式"
     * ]
     * @param $content
     * @return string
     */
    public function tagLink($tag, $content)
    {
        $name = $tag['item'];
        $sql = [];
        $field = "*";
        $limit = 6;
        $order = "id DESC";
        //根据id 读取广告
        if(isset($tag['id']) && !empty($tag['id'])) array_push($sql,"id in ({$tag['id']})") ;
        //读取所需得字段
        if(isset($tag['field']) && !empty($tag['field'])) $field=$tag['field'];
        //读取状态
        if(isset($tag['status']) && !empty($tag['status'])) array_push($sql,"status={$tag['status']}");
        //读取子集数据
        if(isset($tag['pid']) && !empty($tag['pid'])) array_push($sql,"pid in {$tag['pid']}");
        //条数
        if(isset($tag['limit']) && !empty($tag['limit'])) $limit = $tag['limit'];
        //排序
        if(isset($tag['order']) && !empty($tag['order'])) $order = $tag['order'];
        //形式
        if(isset($tag['type']) && !empty($tag['type'])) array_push($sql,"type={$tag['type']}");
        if($sql)
        {
            $where = implode(" and ",$sql);
        }else{
            $where="";
        }
        $parse = '<?php
             $model = new \app\admin\model\system\Link();
             $__LIST__=  $model->where("'.$where.'")->field("'.$field.'")->limit("'.$limit.'")
                ->order("'.$order.'")
                ->select();
         ?>';
        $parse .= '{volist name="__LIST__" id="'. $name .'"}';
        $parse .= $content;
        $parse .= '{/volist}';
        return $parse;
    }


    /**
     * @Notes:关于
     * @User: chenping
     * @email: 375129701@qq.com
     * @param $tag = [
     *      id : 导航id,
     *      status : 状态，
     *      field : 字段
     *      limit : 条数，
     *      order : "排序",
     *      type : "形式"
     * ]
     * @param $content
     * @return string
     */
    public function tagAbout($tag, $content)
    {
        $name = $tag['item'];
        $sql = [];
        $field = "*";
        $limit = 6;
        $order = "id DESC";
        //根据id 读取广告
        if(isset($tag['id']) && !empty($tag['id'])) array_push($sql,"id in ({$tag['id']})") ;
        //读取所需得字段
        if(isset($tag['field']) && !empty($tag['field'])) $field=$tag['field'];
        //读取状态
        if(isset($tag['status']) && !empty($tag['status'])) array_push($sql,"post_status={$tag['status']}");
        //条数
        if(isset($tag['limit']) && !empty($tag['limit'])) $limit = $tag['limit'];
        //排序
        if(isset($tag['order']) && !empty($tag['order'])) $order = $tag['order'];
        //形式
        if(isset($tag['app_type']) && !empty($tag['app_type'])) array_push($sql,"app_type={$tag['app_type']}");
        if($sql)
        {
            $where = implode(" and ",$sql);
        }else{
            $where="";
        }
        $parse = '<?php
             $model = new \app\admin\model\system\About();
             $__LIST__=  $model->where("'.$where.'")->field("'.$field.'")->limit("'.$limit.'")
                ->order("'.$order.'")
                ->select();
         ?>';
        $parse .= '{volist name="__LIST__" id="'. $name .'"}';
        $parse .= $content;
        $parse .= '{/volist}';
        return $parse;
    }
}